'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 士兵升级排程
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');

///////////////////////////////////////////////////////////
/**
 * 技能升级排程
 * @type {mongoose}
 */
var soldierScheduleSchema = new mongoose.Schema({
    // 兵种类型
    soldier_type: {
        type: Number,
        required: true
    },

    // 结束时间
    end_time: {
        type: Date,
        required: true
    }
});

/**
 * 获取士兵类型
 * @return {[type]} [description]
 */
soldierScheduleSchema.methods.getSoldierType = function () {
    return this.soldier_type;
};

/**
 * 士兵升级的结束时间
 * @return {[type]} [description]
 */
soldierScheduleSchema.methods.getEndTime = function () {
    return this.end_time;
};

/**
 * 士兵升级道具卡结束时间
 * @return {[type]} [description]
 */
soldierScheduleSchema.methods.subPropCardEndTime = function (sold_time_value) {
    if (!this.end_time) {
        this.end_time = this.getEndTime() * (1 - sold_time_value / 100);
    }
    return;
};

/**
 * 获取剩余时间
 * @return {[type]} [description]
 */
soldierScheduleSchema.methods.getRemainTime = function () {
    var remain_time = parseInt(moment.duration(this.end_time - moment(), 'ms').asSeconds());
    if (remain_time < 0) {
        return 0;
    }
    return remain_time;
};

if (!soldierScheduleSchema.options.toJSON) {
    soldierScheduleSchema.options.toJSON = {};
}
/* jshint unused:false */
soldierScheduleSchema.options.toJSON.transform = function (doc, ret) {
    ret.end_time = moment(ret.end_time).unix();
};

module.exports = soldierScheduleSchema;
